// program/pages/rentCar/order/index.js
const app = getApp();
const service = require('../../../utils/service.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        agreeChecked: false,
        totalPrice: ''
    },

    bindAgreeClick() {
        this.setData({
            agreeChecked: !this.data.agreeChecked
        })
    },

    switchChange(e) {
        console.log(e)
        let totalPrice = this.data.totalPrice;
        if (e.detail.value) {
            totalPrice = parseFloat(totalPrice) + parseFloat(this.data.orderInfo.optionalServe[e.currentTarget.dataset.idx].price);
        } else {
            totalPrice = parseFloat(totalPrice) - parseFloat(this.data.orderInfo.optionalServe[e.currentTarget.dataset.idx].price);
        }
        this.setData({
            [`orderInfo.optionalServe[${e.currentTarget.dataset.idx}].checked`]: e.detail.value,
            totalPrice: totalPrice
        })
    },

    loadOrderPreviewInfo() {
        let that = this;
        service.getConnect(service.api.getRentCarPreviewOrderInfo, this.data.params).then(res => {
            that.setData({
                orderInfo: res.data,
                totalPrice: res.data.carPrice
            })
            wx.hideLoading();
        })
    },

    submitOrder() {
        let that = this;
        let params = this.data.params;
        params.optionalServe = [];
        for (let i = 0; i < this.data.orderInfo.optionalServe.length; i++) {
            if (this.data.orderInfo.optionalServe[i].checked) {
                params.optionalServe.push(this.data.orderInfo.optionalServe[i].id)
            }
        }
        params.optionalServe = params.optionalServe.join(',');
        service.getConnect(service.api.submitRentCarOrder, this.data.params).then(res => {
            that.setData({
                payParams: res.data
            })
            that.doWxPay(res.data);
            wx.hideLoading();
        })
    },

    // 调起微信支付
  doWxPay(e) {
    var that = this;
    if(e.appId){
      wx.requestPayment({
        timeStamp: e.timeStamp,
        nonceStr: e.nonceStr,
        package: e.package,
        signType: e.signType,
        paySign: e.paySign,
        success(res) {
          console.log(res);
          wx.showToast({
            title: '支付成功~',
            mask: true
          })
          wx.showLoading({
            title: '请稍后...',
            mask: true
          });
          that.checkPayStatus(e.orderSn);
        },
        fail(res) {
          console.log(res);
          wx.redirectTo({
            url: '/pages/order/details/details?orderSn=' + e.orderSn,
          })
        }
      })
    } else {
      wx.showToast({
        title: '支付成功~',
        mask: true
      })
      that.checkPayStatus(e.orderSn);
    }
  },

  checkPayStatus(orderSn) {
    var that = this;
    service.getConnect(service.api.checkOrderPayStatus, { orderSn: orderSn }).then(function (res) {
      console.log(res);
      wx.hideLoading();
      if (res.data.isPay === 1) {
        // 跳转订单详情
        // wx.redirectTo({
        //   url: '/pages/order/details/details?orderSn=' + orderSn,
        // })
        // 跳转支付结果
        wx.redirectTo({
          url: '/pages/order/payResult/index?orderSn=' + orderSn,
        })
      } else {
        setTimeout(function () {
          that.checkPayStatus(orderSn);
        }, 1000);
      }
    })
  },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options.params);
        this.setData({
            params: JSON.parse(options.params)
        })
        this.loadOrderPreviewInfo();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        app.pageOnloadInit(this, app);
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})